package com.koron.car.web.authorized.filter;

import com.alibaba.fastjson.JSON;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.car.web.authorized.bean.vo.DelimitApplyVO;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: CarApplyFilter
 * @projectName GDH_EAM_V2
 * @description: 车辆更购申请工作流插件
 * @date 2022/3/1514:54
 */
@Slf4j
@Component
public class CarReplaceFilter {

    @Autowired
    private SystemFeignService systemFeignService;

    @TaskAnnotation("task0Submitter")
    public List<WorkflowUser> task0Submitter(SessionFactory factory, SubmitterParam submitterParam) {
        JSON.parseObject(submitterParam.getFormVariables(),  DelimitApplyVO.class);
        List<WorkflowUser> users = filter(factory, account(submitterParam), postCode(submitterParam), orgs(submitterParam));
        return CollectionUtils.isEmpty(users) ? new ArrayList<>() : users;
    }


    /**
     * 获取岗位
     * @param submitterParam
     * @return
     */
    private List<String> postCode(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 岗位编码
        return users.stream().filter(row -> row.getType() == 2).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 人员
     * @param submitterParam
     * @return
     */
    private List<String> account(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 人员账号
        return users.stream().filter(row -> row.getType() == 0).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 组织
     * @param submitterParam
     * @return
     */
    private List<String> orgs(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 人员组织
        return users.stream().filter(row -> row.getType() == 1).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 根据岗位和组织查询审批人
     * 人员列表 && 组织列表 && 岗位列表
     * @param factory 会话
     * @param accounts 人员列表
     * @param orgList 组织列表
     * @param postCodes 岗位列表
     * @return
     */
    private List<WorkflowUser> filter(SessionFactory factory, List<String> accounts, List<String> postCodes, List<String> orgList) {
        if(CollectionUtils.isEmpty(postCodes) && CollectionUtils.isEmpty(orgList)) {
            return null;
        }
        PostUserQuery query = new PostUserQuery();
        query.setPostIdList(postCodes);
        query.setOrgIdList(orgList);
        List<PostUserVo> vos = CommonUtils.ok(systemFeignService.queryPostList(query));
        List<WorkflowUser> users = new ArrayList<>();
        vos.stream().forEach(vo -> {
            WorkflowUser user = new WorkflowUser();
            if(!CollectionUtils.isEmpty(accounts) && !accounts.contains(user.getCode())) {
                return;
            }
            user.setCode(vo.getUserAccount());
            user.setName(vo.getUserName());
            users.add(user);
        });
        if (users.size() == 0) {
            return null;
        }
        return users;
    }
}
